<template>
  <div class="index-container">
    <div class="index-header">
      <div style="display: flex;background-color: #074489;">
        <el-menu
          mode="horizontal"
          background-color="#074489"
          text-color="#FFF"
          active-text-color="#FFF"
          style="width: 100%; margin:0 auto;"
          :default-active="activeName"
        >
          <div class="top-left">
          <img :src="pic || defaultLogoImg">
          </div>
          <div class="top-right">
            <el-menu-item :class="['menuItem',currentNav ==1 ?'changeColor':'']" index="home" @click="menuClick('home',1)">首页</el-menu-item>
            <el-menu-item
          :class="['menuItem',currentNav ==2 ?'changeColor':'']"
            index="intro"
            @click="menuClick('intro',2)"
            >中心简介</el-menu-item>
            <el-menu-item
          :class="['menuItem',currentNav ==3 ?'changeColor':'']"
            index="notes"
            @click="menuClick('notes',3)"
            >通知公告</el-menu-item>
            <!-- <el-menu-item
              index="news"
              @click="menuClick('news')"
            >新闻动态</el-menu-item> -->
            <el-menu-item
            index="hcsmng"
            @click="menuClick('hcsmng',4)"
            :class="['menuItem',currentNav ==4 ?'changeColor':'']"
            >规章制度</el-menu-item>
            <!-- <el-menu-item index="login" @click="menuClick('index',5)" :class="['menuItem',currentNav ==5 ?'changeColor':'']" >系统</el-menu-item> -->
            <div class="search">
              <el-input class="searchInput" v-model="searchInput" @keyup.enter.native="toSearchList">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="toSearchList"></i>
              </el-input>
            </div>
            <el-dropdown class="user">
              <img src="../assets/images/avatar.png">
              <div class="redCircle">{{ totalUnread >99 ? '99+': totalUnread }}</div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  v-if="$route.fullPath !=='/hj-user'"
                  icon="el-icon-plus"
                  @click.native="$router.push('/hj-user')"
                >个人中心</el-dropdown-item>
                <el-dropdown-item
                  v-else
                  icon="el-icon-plus"
                  @click.native="$router.push('/hj-home')"
                >首页</el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-plus"
                  @click.native="$router.push('/cabinet/index')"
                >仓库管理</el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-plus"
                  @click.native="open"
                >用户注销</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
       </el-menu>
      </div>
    </div>
    <div class="bg" v-show="activeName == '/hj-home/intro'||activeName == '/hj-home/notes'||activeName == '/hj-home/hcsmng' ">
      <img src="@/assets/images/bg.png" alt="" />
    </div>
    <div :class="[activeName === '/hj-home/home' || activeName === '/hj-home/home-info'? '' :'index-wrapper']">
      <IndexMain v-if="activeName === '/hj-home/index' ||activeName === '/hj-home/login' || $route.fullPath.indexOf('hj-user') !== -1 ||activeName === '/hj-home/home'||activeName === '/hj-home/home-info'|| $route.fullPath === '/hj-home/search-list'" />
      <div v-else class="hasTitleRouterView">
        <div v-if="activeName !== 'platform_intro'" style="width: 20%;">
          <div
          class="header_title"
            v-if="activeName === '/hj-home/intro'"
          >
            <div class="clearfix box-header">
              <!-- <i class="el-icon-mouse" /> -->
              <span>中心简介</span>
            </div>
            <div class="apparatus-cate" @click="navTo('/hj-home/intro')">
              <div class="apparatus-cate__link">
                <span
                  class="click-item"
                >INTRODUCTION</span>
              </div>
            </div>
          </div>
          <div
            v-if="activeName === '/hj-home/notes'"
            class="header_title"
          >
            <div class="clearfix box-header">
              <span>通知公告</span>
            </div>
            <div class="apparatus-cate"  @click="navTo('/hj-home/notes')">
              <div class="apparatus-cate__link">
                <span
                  class="click-item"
                >NOTICE</span>
              </div>
            </div>
          </div>
          <div
            v-if="activeName === '/hj-home/news'"
            class="header_title"
          >
            <div class="clearfix box-header">
              <i class="el-icon-mouse" />
              <span>新闻动态</span>
            </div>
            <div class="apparatus-cate" @click="navTo('/hj-home/news')">
              <div class="apparatus-cate__link">
                <span
                  class="click-item"
                >新闻动态</span>
              </div>
            </div>
          </div>
          <div
            v-if="activeName === '/hj-home/hcsmng'"
            class="header_title"
          >
            <div class="clearfix box-header">
              <!-- <i class="el-icon-mouse" /> -->
              <span>规章制度</span>
            </div>
            <div class="apparatus-cate"   @click="navTo('/hj-home/hcsmng')">
              <div class="apparatus-cate__link">
                <span
                  class="click-item"
                >REGULATIONS</span>
              </div>
            </div>
          </div>
        </div>
        <div v-if="activeName === '/hj-home/intro'" class="introRouterView">
          <breadcrumb platform-type="/hj-home/login" class="introBreadcrumb"/>
          <IndexMain />
        </div>
        <div
          v-show="activeName === '/hj-home/notes' ||activeName === '/hj-home/hcsmng' "
          class="notesandhcsmng"
        >
          <!-- <breadcrumb platform-type="/hj-home/login" /> -->
          <IndexMain />
        </div>
      </div>
    </div>
    <Footer />
  </div>
</template>

<script>
import { getUnreadMsgAPI } from '@/api/web/hjUser.js'
import { getUsers } from '@/api/system/user.js'
import { get } from '@/api/lab.js'
import { getToken } from '@/utils/auth'
import Footer from './website/IndexFooter.vue'
import IndexMain from '../layout-web-index/components/IndexMain'
import Breadcrumb from '@/components/Breadcrumb'
import { encrypt, decrypt } from '@/utils/rsaEncrypt'
import Config from '@/settings'
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import qs from 'qs'
import { getSystemAPI } from '@/api/system/menu.js'
import { mapState } from 'vuex'
import { getConfigInfo } from '@/api/storehousrConfig'
import defaultLogoImg from '@/assets/images/logo_2.png'
// import defaultLogoImg from '@/assets/images/uulab.png'

export default {
  name: 'Login',
  components: {
    Footer,
    IndexMain,
    Breadcrumb,
  },
  data() {
    return {
      totalUnread:0,
      searchInput:'',
      codeUrl: '',
      cookiePass: '',
      activeName: '/hj-home/home',
      dialogLogin: false,
      loginForm: {
        username: 'admin',
        password: '123456',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined,
      isLogin: getToken(),
      pic:'',
      defaultLogoImg:defaultLogoImg,
      baseUrl: process.env.VUE_APP_BASE_API,
    }
  },
  watch: {
    $route(n, o) {
      this.getUnreadMsg()
      if (this.$route.path === '/hj-home') {
      	this.activeName = '/hj-home/login'
      } else {
      	this.activeName = '/hj-home/' + this.$route.meta.tagName
      }
      if (n.path === '/hj-user') { this.$store.commit('hjuser/updatedActiveItem', 0) }
    },
    screenWidth: function(val) {
      this.carouselHeight = this.screenWidth / (1280 / 450) + 'px'
    }
  },
  computed:{
    ...mapState('login',['currentNav']),
    ...mapState('settings',['footerTxt'])
  },
  created() {
    this.activeName = '/hj-home/' + this.$route.meta.tagName
    this.carouselHeight = this.screenWidth / (1280 / 450) + 'px'
    // 获取用户名密码等Cookie
    this.getCookie()
    // token 过期提示
    this.point()
    this.getUnreadMsg()
    this.getSystemList()
    this.getConfig()
   if(this.footerTxt !=  '© XGYD--uuLabs'){
    this.$store.dispatch('settings/changeSetting', {
          key: 'footerTxt',
          value: '© XGYD--uuLabs'
        })
   }

  },
  methods: {
    getConfig(){
      getConfigInfo().then(res => {
        this.pic=this.baseUrl+res.app_home_page_logo
  	})
    },
    async getSystemList() {
      const { content } = await getSystemAPI()
      content.pop() //开发模块隐藏与打开
      this.$store.dispatch('app/setSystemList', content)
    },
    toSearchList(){
      if(this.searchInput){
        this.$router.push({name:'search-list',params:{name:this.searchInput}})
        this.searchInput=""
      }
    },
    async getUnreadMsg(page = 0, size = 10) {
      const { totalElements } = await getUnreadMsgAPI(page, size)
      this.totalUnread = totalElements
    },
    userlogin(){
      this.getCode()
      this.dialogLogin = true
      this.$router.push('/login')
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = res.img
        this.loginForm.uuid = res.uuid
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      let password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === undefined ? '' : password
      password = password === undefined ? this.loginForm.password : password
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        code: ''
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        const user = {
          username: this.loginForm.username,
          password: this.loginForm.password,
          rememberMe: this.loginForm.rememberMe,
          code: this.loginForm.code,
          uuid: this.loginForm.uuid
        }
        if (user.password !== this.cookiePass) {
          user.password = encrypt(user.password)
        }
        if (valid) {
          this.loading = true
          if (user.rememberMe) {
            Cookies.set('username', user.username, {
              expires: Config.passCookieExpires
            })
            Cookies.set('password', user.password, {
              expires: Config.passCookieExpires
            })
            Cookies.set('rememberMe', user.rememberMe, {
              expires: Config.passCookieExpires
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }

          this.$store
            .dispatch('Login', user)
            .then(async () => {
              this.loading = false
              this.dialogLogin = false
              this.$message.success('登录成功')
              Cookies.remove('point')
              this.isLogin = getToken()
              this.$store.dispatch('app/changeSubSystem', this.$store.state.app.systemList[0])
              const res1 = await get()
              this.$store.commit('hjuser/setAllLabs',res1)
              const res = await getUsers()
              this.$store.commit('hjuser/setAllUsers',res.content)
              // this.$router.push({ path: this.redirect || '/' })
            })
            .catch(() => {
              this.loading = false
              this.getCode()
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    point() {
      const point = Cookies.get('point') != undefined
      if (point) {
        this.$notify({
          title: '提示',
          message: '当前登录状态已过期，请重新登录！',
          type: 'warning',
          duration: 5000
        })
        Cookies.remove('point')
      }
    },
    menuClick(name,index) {
      this.$store.commit('login/setCurrentNav',index)
      this.$router.push('/hj-home/' + name)
    },
    open() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.logout()

      })
    },
    logout() {
      this.$store.dispatch('LogOut').then(() => {
        this.$router.push('/login')
      })
    },
    submit(){

    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
body {
  min-height: 100%;
  background: #f4f4f4;
  position: relative;
}
.introBreadcrumb {
  font-size: 25px !important;
}
.changeColor {
  font-weight: 700;
}
.index-container {
  position: relative;
  font-size: 15PX;
  margin: 0 auto;
  min-height: 100%;
}
.bg{
  width: 100%;
  height: 566px;
  img{
    width: 100%;
    height: inherit;
  }
}
.index-header ::v-deep {
  position: relative;
  background-color: #074489;
  height: 90px;
  >div {
    height: 100%;
  }
  .el-menu.el-menu--horizontal{
    height: 100%;
  }
  .top-left {
    display: flex;
    align-items: center;
    position: absolute;
    width: 328px;
    height: 100%;
    margin-left: 139px;
    img {
      width: 100%;
      height: 70px;
    }
  }

  .top-right {
    display: flex;
    position: relative;
    margin-left: 35%;
    height: 100%;
    display: flex;
    align-items: center;
  }

  .el-menu-item {
    font-size: 22px;
    margin-left: 50px;
    height: 100%;
    line-height: 90px;
    padding: 0 20px;
  }

  .menu-sub-title {
    font-size: 17px;
  }
  .search{
    height: 100%;
    display: flex;
    align-items: center;
    margin: 0 50px 0 120px;
    background-color: #074489;
    .searchInput {
    width: 218px;
    height: 44px;
    font-size: 16px;
    i {
      line-height: 32px !important;
    }
  }
  .el-input--small .el-input__inner{
      height: 44px;
      line-height: 44px;
    }
  }

  .user {
    position: relative;
    height: 100%;
    display: flex;
    align-items: center;
     img {
      height: 55px;
      width: 55px;
     }
    .redCircle {
      display: inline-block;
      width: 25px;
      height: 25px;
      background-color: red;
      border-radius: 50%;
      color: #ffffff;
      position: absolute;
      right: -11px;
      top: 15px;
      text-align: center;
      line-height: 25px;
      font-size: 8px;
    }
  }
}

.index-wrapper {
  max-width: 80%;
  margin: 18px auto;
  background: #ffffff;

  .item {
    margin-bottom: 10px;
    width: 100%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .item:last-child {
    margin-bottom: 0;
  }

  .box-card:first-child {
    margin-bottom: 18px;
  }
 .header_title{
    margin-bottom: 18px;
    width: 325px;
    height: 164px;
    background-color: #92C5FF;
    border-radius:0 0 10px 10px;
 }
  .box-header {
    height: 120px;
    width: 325px;
    font-size: 45px;
    font-weight: 500;
    text-align: center;
    color: #ffffff;
    background: #074489;
    border-bottom: 1px solid #2068b0;
    cursor: pointer;
    padding-top: 45px;
    border-radius:0 0 10px 10px;
  }

  .box-header span {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .box-header img {
    vertical-align: middle;
    margin-bottom: 3px;
  }
  .apparatus-cate__link {
    width: 325px;
    height: 40px;
    padding-top: 5px;
    text-align: center;
  }

  .click-item {
    font-size: 25px;
    font-weight: 500;
    -webkit-box-direction: normal;
    padding: 20px;
    color: #FFFFFF;
  }

  .click-item:hover {
    color: #2068b0;
    cursor: pointer;
    text-decoration: underline;
  }
}
 .hasTitleRouterView {
     display: flex;
     width: 100%;
     margin-top: -75px;
 }
 .introRouterView {
  width: 78%;
  margin: 94px 0 0 70px;
 }
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 385px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-code {
  width: 33%;
  display: inline-block;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.user-do {
  font-size: 16px;
  margin-top: -10px;
  margin-bottom: 15px;
}

.user-do:first-child {
  color: #888888;
}

.notesandhcsmng{
  width: 78%;
   margin: 94px 0 0 50px;
}
</style>
